﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web;
using MvcEngine.Core.Website;
using MvcEngine.Core;
using MvcEngine.Core.Extensions;
using MvcEngine.Mvc.Extensions;
using MvcEngine.Core.Content;

namespace MvcEngine.Mvc.ModelBinders
{
    public class PageModelBinder : ModelBinderBase
    {
        public override object Bind(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            HttpRequestBase request = controllerContext.HttpContext.Request;
            var modelState = controllerContext.Controller.ViewData.ModelState;

            Page page = IoC.Resolve<Page>();
            page.Name = request.Form.Get("Name");
            page.Title = request.Form.Get("Title");
            page.Keywords = request.Form.Get("Keywords");
            page.SkinId = request.Form.Get("SkinId").ToNullableGuid();
            page.SkinTemplateId = request.Form.Get("SkinTemplateId").ToNullableGuid();
            page.Description = request.Form.Get("Description");
            page.Header = request.Form.Get("Header");
            page.OrderNumber = request.Form.Get("OrderNumber").ToInt();
            page.MenuTitle = request.Form.Get("MenuTitle");
            page.Visible = GetBool(request.Form.Get("Visible"));
            page.VisibleInMenu = GetBool(request.Form.Get("VisibleInMenu"));

            Validate(page, controllerContext);
            return page;
        }
    }
}
